import { defineConfig } from 'vitepress'
import { copyCodePlugin } from '@vuepress/plugin-copy-code'

// 网站基础 URL - 部署到 GitHub Pages 时需要设置
const base = process.env.NODE_ENV === 'production' ? '/myBlog/' : '/'

// 统计配置 - 从环境变量读取，如果没有则使用空字符串（不启用统计）
const GA_MEASUREMENT_ID = process.env.VITE_GA_MEASUREMENT_ID || ''
const BAIDU_SITE_ID = process.env.VITE_BAIDU_SITE_ID || ''

// 构建 head 配置
const headConfig = [
  ['link', { rel: 'icon', href: `${base}me.png` }],
  ['meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0' }],
  ['meta', { name: 'description', content: 'QA的个人博客 - 学习笔记、技术分享、个人简历' }],
  ['meta', { name: 'keywords', content: '前端开发,学习笔记,技术博客,个人简历' }]
]

// 添加 Google Analytics (GA4)
if (GA_MEASUREMENT_ID) {
  headConfig.push(
    // Google Analytics gtag.js
    ['script', { async: true, src: `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}` }],
    ['script', {}, `
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', '${GA_MEASUREMENT_ID}', {
        page_path: window.location.pathname + window.location.search
      });
    `]
  )
}

// 添加百度统计
if (BAIDU_SITE_ID) {
  headConfig.push(
    ['script', {}, `
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?${BAIDU_SITE_ID}";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    `]
  )
}

export default defineConfig({
  title: '个人博客',
  description: '学习笔记、技术分享、个人简历',
  lang: 'zh-CN',
  
  base,
  
  // 头部配置
  head: headConfig,

  // 主题配置
  themeConfig: {
    // 网站 Logo（VitePress 会自动处理 base 路径）
    logo: '/me.png',
    
    // 导航栏
    nav: [
      { text: '首页', link: '/' },
      { text: '学习笔记', link: '/notes/' },
      { text: '个人简历', link: '/resume/' },
      { text: '项目作品', link: '/projects/' }
    ],

    // 侧边栏
    sidebar: {
      '/notes/': [
        {
          link: '/notes/index',
          items: [
            { text: 'Cursor 开发规则', link: '/notes/engineering/cursor-rules' },
            { text: 'GitHub Pages 部署', link: '/notes/engineering/deployment-guide' },
            { text: '网站访问统计配置', link: '/notes/engineering/analytics' }
          ]
        }
      ]
    },

    // 社交链接
    socialLinks: [
      { icon: 'gitee', link: 'https://gitee.com/the-fields-are-dark' }
    ],

    // 页脚
    footer: {
      copyright: 'Copyright © 2025 QianAn'
    },

    // 搜索
    search: {
      provider: 'local'
    }
  },

  // Markdown 配置
  markdown: {
    lineNumbers: true
  },

  // 插件配置
  plugins: [
    copyCodePlugin({
      selector: '[vp-content] div[class*="language-"] pre',
      showInMobile: true,
      duration: 2000,
      locales: {
        '/': {
          copy: '复制代码',
          copied: '已复制',
        },
      },
    }),
  ]
})
